import { MsgDispatcher } from "../Framework/Network/MsgDispatcher";
import { InputMessageEnum } from "../Framework/Network/InputMessageEnum";
import { VisualEntity } from "../Visual/VisualEntity";

const {ccclass, property} = cc._decorator;

const LONG_PRESS_INTERVAL = 1000;

@ccclass
export default class tileBtn extends cc.Component {

    static movingTileNum: number = 0;

    pressTime:number;
    pressing: boolean;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.pressing = false;
        this.node.on(cc.Node.EventType.TOUCH_START, (event) =>
        {
            this.pressTime = Date.now();
            this.pressing = true;
        });

        
        this.node.on(cc.Node.EventType.TOUCH_END, (event) =>
        {
            let releaseTime = Date.now();
            if(this.pressing && releaseTime - this.pressTime < LONG_PRESS_INTERVAL)
            {
                this.shortPress();
            }
            this.pressing = false;
        });
        
    }

    update(): void
    {
        if(this.pressing)
        {
            let releaseTime = Date.now();
            if(releaseTime - this.pressTime > LONG_PRESS_INTERVAL)
            {
                this.longPress();
                this.pressing = false;
            }
        }
    }

    longPress():void
    {
        if(tileBtn.movingTileNum > 0)
            return;
        let vEntity:VisualEntity = this.node.getComponent("VisualEntity");
        MsgDispatcher.getInstance().dispatchMsg(InputMessageEnum.LongPress, [{"x": vEntity.x, "y": vEntity.y}]);
    }

    shortPress():void
    {
        if(tileBtn.movingTileNum > 0)
            return;
        let vEntity:VisualEntity = this.node.getComponent("VisualEntity");
        MsgDispatcher.getInstance().dispatchMsg(InputMessageEnum.ShortPress, [{"x": vEntity.x, "y": vEntity.y}]);
    }
}
